#include "pensetting.h"
#include "ui_pensetting.h"

#include <QButtonGroup>

PenSetting::PenSetting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PenSetting),
    m_size(PS_NORMAL),
    m_type(ST_NORMAL),
    m_color(Qt::red)
{
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setWindowFlags(Qt::FramelessWindowHint);
    ui->setupUi(this);

    m_sizeGroup = new QButtonGroup(this);
    m_sizeGroup->setExclusive(true);
    m_sizeGroup->addButton(ui->size1,0);
    m_sizeGroup->addButton(ui->size2,1);
    m_sizeGroup->addButton(ui->size3,2);

    connect(m_sizeGroup,static_cast<void(QButtonGroup::*)(QAbstractButton*)>(&QButtonGroup::buttonClicked),this,[=](QAbstractButton *btn){
        if(btn == ui->size1){
            m_size = PS_SMALL;
        }else if(btn == ui->size2){
            m_size = PS_NORMAL;
        }else if(btn == ui->size3){
            m_size = PS_LARGE;
        }else{
            return;
        }

        emit sizeChanged(m_size);
    });

    QButtonGroup* group2 = new QButtonGroup(this);
    group2->setExclusive(true);
    group2->addButton(ui->color1);
    group2->addButton(ui->color2);
    group2->addButton(ui->color3);
    group2->addButton(ui->color4);
    group2->addButton(ui->color5);
    group2->addButton(ui->color6);

    connect(group2,static_cast<void(QButtonGroup::*)(QAbstractButton*)>(&QButtonGroup::buttonClicked),this,[=](QAbstractButton *btn){

        if(btn == ui->color1){
            m_color = QColor(255,52,64);
        }else if(btn == ui->color2){
            m_color = QColor(255,198,46);
        }else if(btn == ui->color3){
            m_color = QColor(69,144,228);
        }else if(btn == ui->color4){
            m_color = QColor(48,187,90);
        }else if(btn == ui->color5){
            m_color = QColor(Qt::black);
        }else if(btn == ui->color6){
            m_color = QColor(Qt::white);
        }else {
            return;
        }

        emit colorChanged(m_color);
    });

    connect(ui->comboBox,static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),this,[=](int index){
        this->m_size = PenSize(index);
        emit sizeChanged(m_size);
    });

    setSettingType(ST_NORMAL);
}

PenSetting::~PenSetting()
{
    delete ui;
}

void PenSetting::setSettingType(SettingType st)
{
    m_type = st;
    if(st == ST_NORMAL){
        this->setFixedWidth(280 * m_dp);
        ui->frame->setVisible(true);
        ui->stackedWidget->setCurrentIndex(0);

        int id = m_sizeGroup->checkedId();
        m_size = (PenSize)id;
    }else if(st == ST_TEXT){
        this->setFixedWidth(280 * m_dp);
        ui->frame->setVisible(true);
        ui->stackedWidget->setCurrentIndex(1);

        m_size = (PenSize)ui->comboBox->currentIndex();
    }else if(st == ST_MOSAIC) {
        this->setFixedWidth(108 * m_dp);
        ui->frame->setVisible(false);
        ui->stackedWidget->setCurrentIndex(0);

        int id = m_sizeGroup->checkedId();
        m_size = (PenSize)id;
    }

    emit sizeChanged(m_size);
}

void PenSetting::setDpi(int dp)
{
    m_dp = dp;

    ui->color1->setFixedSize(14 * m_dp,14 * m_dp);
    ui->color2->setFixedSize(14 * m_dp,14 * m_dp);
    ui->color3->setFixedSize(14 * m_dp,14 * m_dp);
    ui->color4->setFixedSize(14 * m_dp,14 * m_dp);
    ui->color5->setFixedSize(14 * m_dp,14 * m_dp);
    ui->color6->setFixedSize(14 * m_dp,14 * m_dp);

    ui->stackedWidget->setFixedWidth(70 * m_dp);
    ui->size1->setFixedSize(8 * m_dp,8 * m_dp);
    ui->size2->setFixedSize(11 * m_dp,11 * m_dp);
    ui->size3->setFixedSize(14 * m_dp,14 * m_dp);
}
